package com.cray.intelligenthydrologyback.web;

import com.cray.intelligenthydrologyback.client.domain.entity.DeviceFlowDto;
import com.cray.intelligenthydrologyback.client.model.ResponseResult;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;

/**
 * @author Emil
 */
@Controller
@ResponseBody
@RequestMapping("/flow-direction")
@Api(value = "Flow Direction Controller", tags = "数据流向")
public class FlowDirectionController {
    @Autowired
    private WebClient.Builder webClientBuilder;
    @Autowired
    private ObjectMapper objectMapper;

    @GetMapping("/flow-direction/list")
    @ApiOperation(httpMethod = "GET", value = "数据流向设备列表")
    @ApiResponses(value = {
            @ApiResponse(
                    code = 200,
                    message = "Successfully retrieved user",
                    response = String.class,  // 返回类型依然是 String
                    examples = @Example(value = {
                            @ExampleProperty(value = "{\"code\": \"200\", \"message\": \"人脸数据\"}")
                    })
            ),
            @ApiResponse(code = 404, message = "User not found")
    })
    public ResponseResult<DeviceFlowDto> getFlowDirectionList() throws JsonProcessingException {
        Mono<String> response = webClientBuilder.build().get()
                .uri(uriBuilder -> uriBuilder
                        .path("/flow-direction/list")
                        .build()) // 路径变量
                .retrieve()
                .bodyToMono(String.class); // 返回响应体
        return objectMapper.readValue(response.block(), ResponseResult.class);
    }
}
